package cn.stylefeng.guns.sys.modular.system.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyXd;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.BtyXdService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *巡店控制器
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyXd")
public class BtyXdController extends BaseController {

    private String PREFIX = "/modular/system/btyXd/";
    @Resource
    private BtyBxdaService btyBxdaService;
    @Resource
    private BtyXdService btyXdService;
    private String userName="admin";
    @Value("${spring.mvc.view.prefix}")
    private String prefix;
    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyXd.html";
    }
    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String dm) {
        System.out.println("prefix:"+prefix);
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        Long userId=LoginContextHolder.getContext().getUserId();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list;
        String roleName="巡店管理员";
        if(userName.equals(username)|roleNames.contains(roleName)){
            list = this.btyXdService.list(condition,null,dm);
        }else {
            list = this.btyXdService.list(condition,userId,dm);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();

        return LayuiPageFactory.createPageInfo(wrap);

//        if(!(userName.equals(account))){
//            if(rem.contains(off)){
//                list = this.btyXdService.lists(condition,userId,username,null);
//
//            }else{
//                list = this.btyXdService.lists(condition,null,null,username);
//            }
//        }else{
//            list = this.btyXdService.lists(condition,userId,null,null);
//        }
//        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
//        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取巡店检查导出excel列表
     */
    @RequestMapping(value = "/listExport")
    @ResponseBody
    public Object listExport(String condition,String dm) {
        //获取用户的帐号
        String username=LoginContextHolder.getContext().getUser().getName();
        Long userId=LoginContextHolder.getContext().getUserId();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        List<Map<String, Object>> list;
        String roleName="巡店管理员";
        if(userName.equals(username)|roleNames.contains(roleName)){
            list = this.btyXdService.listExport(condition,null,dm);
        }else {
            list = this.btyXdService.listExport(condition,userId,dm);
        }

        return list;
    }
    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyXd_add")
    public String noticeAdd() {
        return PREFIX + "btyXd_add.html";
    }
    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyXd_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyXd btyXd = this.btyXdService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyXd));
        LogObjectHolder.me().set(btyXd);
        return PREFIX + "btyXd_edit.html";
    }
    /**
     * 修改档案
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyXd btyXd,@RequestParam String qjwsdf,@RequestParam String yrybdf,@RequestParam String fwlcdf
            ,@RequestParam String dzdpdf,@RequestParam String xqdf,@RequestParam String cppzdf,@RequestParam String zf) {
        if (ToolUtil.isOneEmpty(btyXd,btyXd.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        btyXd.setSumQjws(qjwsdf);
        btyXd.setSumYryb(yrybdf);
        btyXd.setSumFwlc(fwlcdf);
        btyXd.setSumDzdp(dzdpdf);
        btyXd.setSumXq(xqdf);
        btyXd.setSumCppz(cppzdf);
        btyXd.setZf(zf);
        this.btyXdService.updateById(btyXd);
        return SUCCESS_TIP;
    }

    /**
     * 新增报销档案
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BtyXd btyXd,@RequestParam String qjwsdf,@RequestParam String yrybdf,@RequestParam String fwlcdf
            ,@RequestParam String dzdpdf,@RequestParam String xqdf,@RequestParam String cppzdf,@RequestParam String zf) {
        if (ToolUtil.isOneEmpty(btyXd)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
//        SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
//        String bh = df.format(new Date());
//        //替换大部分空白字符,不限于空格
//        String yhk=btyBxda.getYhk().replaceAll("\\s*", "");
//        btyBxda.setYhk(yhk);
//        btyBxda.setBh(bh);
        btyXd.setSumQjws(qjwsdf);
        btyXd.setSumYryb(yrybdf);
        btyXd.setSumFwlc(fwlcdf);
        btyXd.setSumDzdp(dzdpdf);
        btyXd.setSumXq(xqdf);
        btyXd.setSumCppz(cppzdf);
        btyXd.setZf(zf);
        btyXd.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyXd.setCreateTime(new Date());
        this.btyXdService.save(btyXd);
        return SUCCESS_TIP;
    }
    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        BtyXd btyXd=this.btyXdService.getById(id);
        String account= LoginContextHolder.getContext().getUser().getName();
//        if(account.equals("admin")){
//            btyBxda.setRoleFlag(0);
//        }else{
//            btyBxda.setRoleFlag(1);
//        }
        return btyXd;
    }
    /**
     * 删除报销档案
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyXdService.removeById(id);
        return SUCCESS_TIP;
    }
    /**
     * 查询店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyXd> getDm(){
        String username=LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        String roleName="巡店管理员";
        List<BtyXd> list;
        if(userName.equals(username)|roleNames.contains(roleName)){
          list=btyXdService.dmList(null);
        }else{
            list=btyXdService.dmList(username);
        }
        return list;
    }

}
